﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.Doc;
using Enterprise.Data.Doc;
using Enterprise.Infrastructure;
namespace Enterprise.Service.Doc
{
    public class DocRecevieService
    {
        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-DocRecevie";

        /// <summary>
        ///  会议签收数据列表
        /// </summary>
        /// <returns></returns>
        public List<DocRecevieEntity> DocRecevieList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<DocRecevieEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                DocRecevieData data = new DocRecevieData();
                List<DocRecevieEntity> list = data.DocRecevieList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 判断人员是否签收会议记录
        /// </summary>
        /// <param name="mId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool DocRecevieCheck(int mId, int userId)
        {
            bool rbool = false;
            var q = DocRecevieList().Where(p => p.DID == mId && p.RUSERID == userId).FirstOrDefault();
            if (q != null)
            {
                rbool = true;
            }
            return rbool;
        }

        /// <summary>
        /// 会议签收 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void DocRecevieExecute(DocRecevieEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into DOC_RECEVIE(RID,DID,RUSERID,RTIME) values(SEQ_DOC_RECEVIE.nextval," + entity.DID + "," + entity.RUSERID + "," + Utility.FormatOracleDate(entity.RTIME) + ")";
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                ExecuteBySql(cmd);
            }
        }

        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void ExecuteBySql(string cmd)
        {
            DocRecevieData data = new DocRecevieData();
            data.Execute(cmd);
            ClearCache();
        }
    }
}
